#include "stdafx.h"
#include "VSCtxMenuFixApp.h"
#include "..\VSCtxMenuFixHook\VSCtxMenuFixHook.h"

static WCHAR g_szWindowClassName[] = L"VSCtxMenuFixApp";

CMyApp::CMyApp()
{
    m_Success = FALSE;
    m_cAppMutex = NULL;
    m_hWnd = NULL;
    m_cTrayIcon = NULL;

    m_cAppMutex = new CMyAppMutex();

    if (!m_cAppMutex->Success())
    {
        return;
    }

    MyRegisterClass();

    m_hWnd = MyCreateWindow();

    if (!m_hWnd)
    {
        return;
    }

    m_cTrayIcon = new CMyTrayIcon(m_hWnd);

    if (!m_cTrayIcon->Success())
    {
        return;
    }

    if (!SetHook())
    {
        return;
    }

    m_Success = TRUE;
}

CMyApp::~CMyApp()
{
    RemoveHook();

    if (m_cTrayIcon)
    {
        delete m_cTrayIcon;
    }

    if (m_cAppMutex)
    {
        delete m_cAppMutex;
    }
}

ATOM CMyApp::MyRegisterClass()
{
    WNDCLASSEX wcex;

    ZeroMemory(&wcex, sizeof(WNDCLASSEX));
    wcex.cbSize = sizeof(WNDCLASSEX);

    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc = AppWndProc;
    wcex.cbClsExtra = 0;
    wcex.cbWndExtra = 0;
    wcex.hInstance = g_hInst;
    wcex.hIcon = LoadIcon(g_hInst, MAKEINTRESOURCE(IDI_APP));
    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
    wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
    wcex.lpszMenuName = NULL;
    wcex.lpszClassName = g_szWindowClassName;
    wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_APP));

    return RegisterClassEx(&wcex);
}

HWND CMyApp::MyCreateWindow()
{
    return CreateWindow(
        g_szWindowClassName,
        CMyResources::GetAppTitle(),
        WS_POPUP,
        0, 0, 100, 100,
        NULL,
        NULL,
        g_hInst,
        NULL);
}
